﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

using System.Linq;
using System.Threading.Tasks;
using WebAppCoreDI.Data;
using WebAppCoreDI.Interface;

namespace WebAppCoreDI.Models
{
    public class CharacterRepository: ICharacterRepository
    {
        private readonly ApplicationDbContext _dbContext;

        public CharacterRepository() { }

        public CharacterRepository(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        public IEnumerable<Character> ListAll()
        {
            return _dbContext.Characters.AsEnumerable();
        }

        public void Add(Character character)
        {
            _dbContext.Characters.Add(character);
            _dbContext.SaveChanges();
        }

        public virtual Task<List<Character>> ListAsync()
        {
            return _dbContext.Characters                
                .OrderByDescending(s => s.Name)
                .ToListAsync();
        }

        public Task AddAsync(IList<Character> characters)
        {
            foreach (var cha in characters)
            {
                _dbContext.Characters.Add(cha);
            }
            return _dbContext.SaveChangesAsync();
        }
    }
}
